import {
	getPageConfig,
	getConfig
} from '@/api/common'

export default {
	namespaced: true,
	state: {
		info: {},
		pageConfig: [],
		timer: null,
		nowTimestamp: new Date().getTime()
	},
	mutations: {
		SET_PAGECONF(state, data) {
			state.pageConfig = data;
		},
		SET_BASECONFIG(state, data) {
			state.info = data;
		},
		CREATE_TIMER(state) {
			if (state.timer) return;
			state.timer = setInterval(() => {
				state.nowTimestamp = new Date().getTime()
			}, 1000)
			console.log("全局更新时间timer已启动");
		}
	},
	actions: {
		createNowTimeTimer({
			state,
			commit
		}) {
			commit('CREATE_TIMER')
		},

		getConfig({
			state,
			commit
		}, key) {
			let keys = [
				'resell_collection', 'copyright', 'transfer_collection', 'appLogo', 'appBigLogo', 'pay_audit',
				"app_version",
				'appName',
				'puzzleCaptcha',
				'cancellation',
				// 'aKeyToLogIn',
				// 'aKeyToLogInKey',
				// 'aKeyToLogInImage',
				'pay_time_limit',
				'h5_domain',
				'draw_notice',
				'credit_goods_dhxz',
				'credit_goods_zysx',
				'service_image',
				'service_mobile',
				'certification',
				'consignment_sales',
				'isAgreement',
				'h5_invite_url',
				'strategy',
				"aboutus",
				'ipa',
				'default_pay_type',
				'fastOrder_check',
				"batchBuy_check",
				'normal_check',
				'synthetic_check',
				'service_email',
				'draw_share_assrule',
				'draw_code_rankrule',
				'draw_lots_rule',
				'draw_lots_notice',
				'share_rule',
				'score_helpdraw_sub',
				'helpdraw_rule',
				'draw_share_assimage',
				'comm_desc',
				'risk_warning',
				'qq_team_num',
				'olduser_dayhelp_num',
				'purchase_sale_details',
				'jyxingweiid',
				'share_bac_iamge',
				'search_bsn_url',
				'trade_rule',
				'platform_resale_rate',
				'beg_sale_rule',
				'price_trend_status',
				'resale_img',
				'draw_url',
				'activity_url',
				'open_creen_status',
				'open_creen_img',
				'ali_app_login_status',
				'ali_h5_login_status',
				'index_image1',
				'index_url1',
				'index_image2',
				'index_url2',
				'index_image3',
				'index_url3',
				'service_agreement'
			]
			return getConfig(keys.join(',')).then(res => {
				console.log('基础配置加载成功')
				commit('SET_BASECONFIG', res.data)
				// if (res.data.open_creen_status == 1 && uni.getStorageSync('isOpenCreen') != 1) {
				// 	uni.redirectTo({
				// 		url: "/pages/openCreen/openCreen"
				// 	})
				// }
				uni.setStorageSync('isOpenCreen', res.data.open_creen_status)

			})
		},
		appInit({
			dispatch
		}) {
			dispatch('checkNetwork').then(res => {
				if (res) {
					dispatch('getConfig');
				} else {
					uni.redirectTo({
						url: "/pages/networkError/networkError"
					})
				}
			})
			dispatch("listenNetwork");
		},
		checkNetwork() {
			return new Promise((reslvoe) => {
				uni.getNetworkType({
					success: function(res) {
						if (res.networkType === "none") {
							reslvoe(false)
						} else {
							reslvoe(true)
						}
					}
				});
			})
		},
		listenNetwork({
			dispatch
		}) {
			uni.onNetworkStatusChange((res) => {
				if (res.isConnected) {
					dispatch("getConfig")
					let getCurrentPagesArr = getCurrentPages();
					let currentPage = getCurrentPagesArr[getCurrentPagesArr.length - 1].route
					if (currentPage != 'pages/networkError/networkError') return
					// #ifdef APP-PLUS
					plus.runtime.restart();
					// #endif
					// #ifdef H5
					uni.switchTab({
						url: '/pages/index/index'
					})
					// #endif
				} else {
					uni.redirectTo({
						url: "/pages/networkError/networkError"
					})
				}
			})
		},
		getPageConfig({
			commit,
			dispatch
		}) {
			return new Promise((reslove, reject) => {
				getPageConfig().then(res => {
					console.log('页面配置加载成功')
					res.data = res.data.map(v => {
						v.content = JSON.parse(v.content)
						return v
					})
					commit('SET_PAGECONF', res.data)
					reslove(res.data)
				})
			})
		}
	}
}